﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Model;
using SerialPortBll;

namespace 串口调试助手
{
    public partial class SaveCmd : Form
    {
        public SendDataLog SendDataLogObj { get;}

        public event Action<SendDataLog> OnSavedSendDataLog;

        public SaveCmd(SendDataLog sendDataObj)
        {
            InitializeComponent();
            SendDataLogObj = sendDataObj;
        }

        private void SaveCmd_Load(object sender, EventArgs e)
        {
            if (SendDataLogObj != null)
            {
                this.richTextBox1.Text = SendDataLogObj.Send_data_content;

                if(SEND_MODE.HEX.ToString().Equals( SendDataLogObj.Send_mode ))
                {
                    radioButton1.Checked = true;
                    radioButton2.Checked = false;
                }else
                {
                    radioButton1.Checked = false;
                    radioButton2.Checked = true;
                }
            }
            initGroupData();
        }

        private void initGroupData()
        {
            radDropDownList1.Items.Clear();
            radDropDownList1.DataSource = WorkGroupBMO.getAll();
            radDropDownList1.DisplayMember = "group_name";
            radDropDownList1.ValueMember = "group_no";
            //radDropDownList1.DescriptionTextMember = "note";
        }


        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(textBox1.Text))
            {
                MessageBox.Show("名称不能为空！");
                return;
            }
            else
            {
                SendDataLog obj = SendDataLogBMO.selectObjByName(textBox1.Text);
                if (obj != null)
                {
                    MessageBox.Show("该名称已存在，请修改！","名称重复", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            if (String.IsNullOrWhiteSpace(richTextBox1.Text))
            {
                MessageBox.Show("内容不能为空！");
                return;
            }
            try
            {
                this.Cursor = Cursors.WaitCursor;
                SendDataLogObj.Send_name = textBox1.Text;
                SendDataLogObj.Send_data_content = richTextBox1.Text;
                SendDataLogObj.Group_no = (int)radDropDownList1.SelectedValue;
                SendDataLogObj.Status = STATUS.在用.ToString();
                 
                if (radioButton1.Checked)//SEND_MODE.HEX.ToString().Equals(SendDataLogObj.Send_mode))
                {
                    SendDataLogObj.Send_mode = SEND_MODE.HEX.ToString();
                }
                else
                {
                    SendDataLogObj.Send_mode = SEND_MODE.ASCII.ToString();
                }

                SendDataLogBMO.save(SendDataLogObj);
                // MessageBox.Show("保存成功！");
                OnSavedSendDataLog?.Invoke(SendDataLogObj);
                this.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show("保存失败！");
                Global.log.Error("保存失败！:",ex);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            AddGroupFrm af = new AddGroupFrm();          
            af.OnAddGrouped += Af_OnAddGrouped;
            af.ShowDialog();
        }

        private void Af_OnAddGrouped(WorkGroup obj)
        { 
            initGroupData();
            foreach(var v in radDropDownList1.Items)
            {
                if(obj.Group_name.Equals(v.Text))
                {
                    v.Selected = true;
                    break;
                }
            } 
        }
    }
}
